package com.game.betLottery.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.eova.common.base.BaseModel;
import com.jfinal.plugin.activerecord.Db;

public class Lottery extends BaseModel<Lottery>{
	private static final long serialVersionUID = 1L;
	
	
	public static final Lottery dao = new Lottery();
	
	/**
	 * 开奖
	 * @param uid
	 * @return
	 */
	public int UpGameDetail(int touMoney,int recoMoney,int gameId) {
		StringBuffer querySql = new StringBuffer(); //押中的
		querySql.append("SELECT gd.id FROM   game_info gi INNER JOIN   game_detail_info  gd ");
		querySql.append(" ON gi.id=gd.gameId WHERE userBet=lotteryResults and  gi.id=?");
		List<Lottery> lotList = this.find(querySql.toString(), 1);
		String id ="";
		for(int i=0;i<lotList.size();i++){
			if(i==lotList.size()-1){
				id += lotList.get(i).get("id").toString();
			}else{
				id += lotList.get(i).get("id").toString()+",";
			}
		}
		
		StringBuffer sql = new StringBuffer();  //押中
		sql.append(" update game_detail_info ");   
		sql.append(" set isWinning ='1',incomeOrExpenditure="+recoMoney+""); 
		sql.append(" where gameId = ? AND id IN("+id+") "); 
		Db.update(sql.toString(),1); 
		
		
		
		StringBuffer querySqlNo = new StringBuffer(); //没押中的
		querySqlNo.append("SELECT gd.id FROM   game_info gi INNER JOIN   game_detail_info  gd ");
		querySqlNo.append(" ON gi.id=gd.gameId WHERE userBet !=lotteryResults and  gi.id=?");
		List<Lottery> lotListNo = this.find(querySqlNo.toString(), 1);
		String idNo ="";
		for(int i=0;i<lotListNo.size();i++){
			if(i==lotListNo.size()-1){
				idNo += lotListNo.get(i).get("id").toString();
			}else{
				idNo += lotListNo.get(i).get("id").toString()+",";
			}
		}
		
		StringBuffer sqlNo = new StringBuffer();  //没押中
		sqlNo.append(" update game_detail_info ");   
		sqlNo.append(" set isWinning ='0',incomeOrExpenditure="+-10+""); 
		sqlNo.append(" where gameId = ? AND id IN("+idNo+") "); 
		Db.update(sqlNo.toString(),1); 
		
		return 0;
	}
}